package javaweb.web;

import javaweb.dao.impl.BookDaoImpl;
import javaweb.pojo.Book;
import javaweb.pojo.Page;
import javaweb.servlet.impl.BaseServlet;
import javaweb.servlet.impl.BookServletImpl;
import javaweb.utility.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author shkstart
 * @creat -07-26 22:44
 */
public class BookServlet extends BaseServlet {
 BookServletImpl bs =new BookServletImpl();
    public void add(HttpServletRequest request, HttpServletResponse response) {
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
       // System.out.println("添加前");
        bs.addBook(book);
        System.out.println(book);
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 0);
        pageNo+=1;

        try {
            response.sendRedirect(request.getContextPath()
                    +"/manager/bookServlet?action=page&pageNo="+pageNo);
        } catch (Exception e) {
            e.printStackTrace();
        }


   }

    /**
     * 处理分页功能
     * @param request
     * @param response
     */
    public void page(HttpServletRequest request, HttpServletResponse response) {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Book> page = bs.page(pageNo, pageSize);
        page.setUrl("manager/bookServlet?action=page");
        request.setAttribute("page",page);



        try {
            request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void upDate(HttpServletRequest request, HttpServletResponse response) {

        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        System.out.println(book);
     bs.upDateBook(book);
        try {
            response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void getBook(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        Book book = bs.queryBookId(i);
        request.setAttribute("book",book);
        try {
            request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,
                    response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void deleteBookId(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        bs.detleteBookId(i);
        try {
            response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void queryBookId(HttpServletRequest request, HttpServletResponse response) {

    }public void queryBooks(HttpServletRequest request, HttpServletResponse response) {
        List<Book> books = bs.qurtyBooks();
         request.setAttribute("books", books);
        try {
            request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
